package com.chixing.controller;

import com.chixing.pojo.Customer;
import com.chixing.pojo.HotelInfo;
import com.chixing.pojo.HotelRoomInfo;
import com.chixing.pojo.LoginReqParams;
import com.chixing.service.CustomerService;
import com.chixing.service.HotelRoomTypeService;
import com.chixing.service.HotelService;
import com.chixing.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.sound.midi.Soundbank;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

@RestController
public class HotelController {
    @Autowired
    CustomerService customerService;

    @Autowired
    HotelService hotelService;

    @Autowired
    HotelRoomTypeService hotelRoomTypeService;

    @RequestMapping("getByCity")
    public ServerResponse getByCity(@RequestBody String hotelCity, HttpSession session){
        Object loginCustomer = session.getAttribute("loginCustomer");
        System.out.println(loginCustomer);
        if (loginCustomer==null){
            return null;
        }else {
            List<HotelInfo> hotelInfoList = hotelService.getByHotelCity(hotelCity);
            session.setAttribute("loginCustomer",loginCustomer);
            session.setAttribute("cityName",hotelCity);
            return ServerResponse.getDataSuccess(hotelInfoList);
        }
    }


    @RequestMapping("getHotel")
    public ServerResponse getHotel(@RequestBody String hotelCity, HttpSession session){
        List<HotelInfo> hotelList = new ArrayList<>();
        List<HotelInfo> hotelInfoList = hotelService.getByHotelCity(hotelCity);
        for (HotelInfo hotelInfo : hotelInfoList) {
            HotelInfo hotelInfo1 = new HotelInfo();
            Integer hotelId = hotelInfo.getHotelId();
        }
        return null;
    }

    @RequestMapping("getSession")
    public ServerResponse getSession(@RequestBody String hotelName,HttpSession session) throws UnsupportedEncodingException {
        Object loginCustomer = session.getAttribute("loginCustomer");

        if (loginCustomer==null) {
            return null;
        }else {
            session.setAttribute("loginCustomer",loginCustomer);
            session.setAttribute("hotelName",hotelName);
            return ServerResponse.getDataSuccess(loginCustomer);
        }
    }

    @RequestMapping("getByName")
    public ServerResponse getByName(@RequestBody String hotelName,HttpSession session){
        System.out.println(hotelName);
        List<HotelInfo> byName = hotelService.getByName(hotelName);
        if (byName.size()==0){
            return null;
        }
        return ServerResponse.getDataSuccess(byName);
    }

    @RequestMapping("getRoomTypeById")
    public ServerResponse getRoomTypeById(@RequestBody String hotelName,HttpSession session){
        List<HotelRoomInfo> rooms = hotelRoomTypeService.getByName(hotelName);
        if (rooms.size() == 0){
            return null;
        }
        return ServerResponse.getDataSuccess(rooms);
    }

    @RequestMapping("getSession1")
    public ServerResponse getSession1(@RequestBody Float prices,HttpSession session) throws UnsupportedEncodingException {
        Object loginCustomer = session.getAttribute("loginCustomer");

        if (loginCustomer==null) {
            return null;
        }else {
            session.setAttribute("loginCustomer",loginCustomer);
            session.setAttribute("prices",prices);
            return ServerResponse.getDataSuccess(loginCustomer);
        }
    }

}
